
#include "common_api.h"




void timer_init(void)
{
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 500);		//2ms
    TIMER_EnableInt(TIMER0);

    TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 500);		//2ms
    TIMER_EnableInt(TIMER1);

    TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 100);		//10 ms
    TIMER_EnableInt(TIMER2);

//	TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, 1000);     

    /* Enable Timer0 ~ Timer3 NVIC */
    NVIC_EnableIRQ(TMR0_IRQn);
    NVIC_EnableIRQ(TMR1_IRQn);
    NVIC_EnableIRQ(TMR2_IRQn);

	TIMER_Start(TIMER0);
    TIMER_Start(TIMER1);
    TIMER_Start(TIMER2);	
    
}
void Delay_us(uint16_t i)
{
	uint16_t j=0;
  
	while(i--)
	{
		for(j=0;j<5;j++)//??????
		{
			j=j;		
		}
	}
    
}
void Delay_ms(uint16_t i)
{
	WDT_Clr();
	while(i--)
	{
		Delay_us(10);
	}
    
}

void WorkingTime(void)
{
	uint8_t DataBuf[4];

	if( TimeCount )	
	{
		Work_Time_Count++;	

		if (Work_Time_Count < 4 )
		{
				if(Work_Time_Count==1)//32位单片机
				{	
					DataBuf[0]= (Monitor_Work_Time>>24)&0xFF;	//高八位
					DataBuf[1]= (Monitor_Work_Time>>16)&0xFF;
					DataBuf[2]= (Monitor_Work_Time>>8)&0xFF;
					DataBuf[3]= Monitor_Work_Time&0xFF;				//低八位
					Write_Data(0, DataBuf[0]);		
					Write_Data(1, DataBuf[1]);		
					Write_Data(2, DataBuf[2]);		
					Write_Data(3, DataBuf[3]);		
				}

		}
		else if( Work_Time_Count >= 360 )//CESHI 360
		{
			Work_Time_Count=0;	
			if ( Monitor_Work_Time < 999998 )
			{
				Monitor_Work_Time++;	//六分钟加1
			}
		}
	}
}
/**
 * @brief       Timer0 IRQ
 *
 * @param       None
 *
 * @return      None
 *
 * @details     The Timer0 default IRQ, declared in startup_NUC131.s.
 */
void TMR0_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER0) == 1)
    {
        /* Clear Timer0 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER0);
		
        //在定时器里吃掉步长，一个步长，八个节拍
        if(MotorA.Step   &&  (MotorA.Direction ==1))    //有步长且方向为正
        {
            A_Rotate(MotorA.Beat);
            MotorA.Beat --;
            if(MotorA.Beat ==0) MotorA.Step --;     //八个拍减一个步长
        }
        else if(MotorA.Step   &&  (MotorA.Direction ==0)) //有步长且方向为反
        {
            A_Rotate(MotorA.Beat);
            MotorA.Beat ++;
            if(MotorA.Beat ==0) MotorA.Step --;     //八个拍减一个步长
        }
        else
        {
            //无步长，无动作
            MotorA.Step = 0;
        }		       
    }
}

/**
 * @brief       Timer1 IRQ
 *
 * @param       None
 *
 * @return      None
 *
 * @details     The Timer1 default IRQ, declared in startup_NUC131.s.
 */
void TMR1_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER1) == 1)
    {
        /* Clear Timer1 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER1);
		
        //在定时器里吃掉步长，一个步长，八个节拍
        if(MotorB.Step   &&  (MotorB.Direction ==1))    //有步长且方向为正
        {
            B_Rotate(MotorB.Beat);
            MotorB.Beat --;
            if(MotorB.Beat ==0) MotorB.Step --;     //八个拍减一个步长
        }
        else if(MotorB.Step   &&  (MotorB.Direction ==0)) //有步长且方向为反
        {
            B_Rotate(MotorB.Beat);
            MotorB.Beat ++;
            if(MotorB.Beat ==0) MotorB.Step --;     //八个拍减一个步长
        }
        else
        {
            //无步长，无动作
            MotorB.Step = 0;
        }
        
    }
}

/**
 * @brief       Timer2 IRQ
 *
 * @param       None
 *
 * @return      None
 *
 * @details     The Timer2 default IRQ, declared in startup_NUC131.s.
 */
void TMR2_IRQHandler(void)	//10ms 进入一次
{
    if(TIMER_GetIntFlag(TIMER2) == 1)
    {
        /* Clear Timer2 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER2);
//		Time10msCnt++;
//		Time30msCnt++;
		Time100msCnt++;
		Time300msCnt++;
		Time500msCnt++;
		Time1s++;
		if( Time1s>=100)
		{
			Time1s = 0;
			
			Time1sFlag = 1;
		}

    }
}


